import { getH5LoginInfo } from '@/common/api'
import type { AnyObject } from '@/common/types'

export async function getWxSignature():Promise<AnyObject> {
  // const url = location.hostname
  const url = 'https://xuefa.qdyuyue.com/'
  const context = getCurrentInstance()
  const proxy = context?.proxy
  const res = await getH5LoginInfo(url)
  const { data } = res
  return new Promise((resolve, reject) => {
    proxy?.$wx.config({
      debug: false,
      appId: data.appId,
      timestamp: data.timestamp,
      nonceStr: data.nonceStr,
      signature: data.signature,
      jsApiList: [
        'chooseWXPay',
        'login',
        'updateAppMessageShareData',
        'updateTimelineShareData'
      ],
      openTagList: ['wx-open-launch-weapp']
    })
    proxy?.$wx.ready(() => {
      resolve({
        code: 200,
        msg: 'success',
        data: {
          appId: data.appId
        }
      })
    })
    proxy?.$wx.error((err:any) => {
      console.log(err)
    })
  })
}
